﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FDSimpleModelGenerator.Entities;
using FDSimpleModelGenerator.Entities.CreateTables;
using FDSimpleModelGenerator.IRepositories;
using FDSimpleModelGenerator.IServices;

namespace FDSimpleModelGenerator.Repositories
{
    /// <summary>
    /// 列索引板仓储
    /// </summary>
    public class ColumnIndexRepository : BasicRepository<ColumnIndex>, IColumnIndexRepository
    {
        public ColumnIndexRepository(DbConnectionInfo dbConnection, IDbAccessor dbAccessor, IGenerateDbSql generateDbSql)
            : base(dbConnection, dbAccessor, generateDbSql)
        {
        }

        public async Task<bool> AddBatchAsync(IEnumerable<ColumnIndex> indices, int tableId)
        {
            await BeginTransaction();
            try
            {
                foreach (var index in indices)
                {
                    if (!await AddAsync(index))
                    {
                        await RollbackTransaction();
                        return false;
                    }
                }

                return await CommitTransaction();
            }
            catch (Exception e)
            {
                await RollbackTransaction();
                return false;
            }
        }
    }
}
